<?php
/**
 * Created by PhpStorm.
 * User: Максим
 * Date: 23.07.13
 * Time: 17:19
 */

include 'simple_html_dom.php';
ini_set("memory_limit", "2024M");

function get_url($link, $model)
{
    $model = urlencode($model);
    $link = str_ireplace('exist.ua', 'www.elcats.ru', $link);
    $link = str_ireplace('Parts.axd', 'fullinfo.aspx', $link);
    $url = $link . "&marka=$model&office=ru";
    return $url;
}

function insert_model($model, $years, $url, $oe, $marka)
{
    $marka_id = null;
    $oe_id = null;

    global $db;

    $result = $db->query("SELECT `id` FROM `ex_marka` WHERE marka = '$marka'");
    if ($result->num_rows > 0)
    {
        $marka_id = $result->fetch_object()->id;
    }
    else
    {
        $db->query("INSERT INTO `ex_marka` (`marka`) VALUES ('$marka')");
        $marka_id  = $db->insert_id;
    }
    $result = null;

    $result = $db->query("SELECT `id` FROM `ex_oe_codes` WHERE oe = '$oe' AND marka_id = '$marka_id'");
    if ($result->num_rows > 0)
    {
        $oe_id = $result->fetch_object()->id;
    }
    else
    {
        $db->query("INSERT INTO `ex_oe_codes` (`oe`, `marka_id`) VALUES ('$oe', '$marka_id')");
        $oe_id = $db->insert_id;
    }
    $result = null;

    //27.08.1999 - 26.12.2000

    $tmp_years = '';

    $parts = explode('-', $years);

    $line1 = trim($parts[0]);
    $line2 = trim($parts[1]);

    $date_parts = explode('.', $line1);

    $tmp_years .= $date_parts[1] . '.' . $date_parts[2] . ' - ';

    if ($line2 != '...')
    {
        $date_parts = explode('.', $line2);

        $tmp_years .= $date_parts[1] . '.' . $date_parts[2];
    }

    $years = $tmp_years; $tmp_years = null;





    $result = $db->query("
        SELECT `id` FROM `ex_model`
        WHERE `model` = '$model'
        AND `link` = '$url' AND `marka_id` = '$marka_id'
        AND `years` = '$years' AND `oe_id` = '$oe_id'");

    if ($result->num_rows == 0)
    {
        $db->query("
            INSERT INTO `ex_model`
                (`model`, `link`, `marka_id`, `years`, `oe_id`)
            VALUES
                ('$model', '$url', '$marka_id', '$years', '$oe_id')");
        print "New - " . $model . " - " . $years . /*" - " . $url . " - " . $oe . " - " . $marka .*/"\n";
    }
    else
    {
        //print "Exist - " . $model . " - " . $years . " - " . $url . " - " . $oe . " - " . $marka ."\n";
    }
}

$db = new mysqli('avtopro.msgroup.ua', 'u_avtopro2', 'H1AuXxH6', 'avtopro');


//$result = $db->query("SELECT * FROM `ex_app_links` WHERE (bad IS NULL OR bad <> 1) AND `manufacture` = 'Hyundai/KIA'");
$result = $db->query("
                SELECT * FROM `ex_app_links`
                WHERE (bad IS NULL OR bad <> 1)
                  AND `manufacture` = 'Hyundai/KIA'
                  AND ex_app_links.`oe` NOT IN (
                  SELECT `ex_oe_codes`.`oe` FROM `ex_model`
                  INNER JOIN `ex_oe_codes` ON (`ex_model`.`marka_id` = `ex_oe_codes`.`marka_id`)
                  AND (`ex_model`.`oe_id` = `ex_oe_codes`.`id`))");


while ($record = $result->fetch_object()):

    $html = new simple_html_dom($record->html);

    $maker_string = $html->find('#lblModels', 0)->innertext;
    $tmp_maker = str_ireplace('&nbsp;', '', explode("|", $maker_string));

    $makers = array();

    foreach ($tmp_maker as $maker)
    {
        $maker = strip_tags($maker);
        $makers[] = trim($maker);
    }

    foreach ($makers as $maker):
        $url = get_url($record->link, $maker);

        $html = file_get_html($url);

        $tblTable = $html->find('#tblTable tr');

        if (is_null($tblTable))
            continue;
        else
        {
            array_shift($tblTable);
            foreach ($tblTable as $tr)
            {
                $td_model = strip_tags($tr->children(0));
                $td_years = strip_tags($tr->children(2));

                insert_model($td_model, $td_years, $url, $record->oe, $maker);
            }
        }
    endforeach;

    /*$html = file_get_contents($record->link);
    $html = $db->real_escape_string($html);
    $db->query("UPDATE `ex_app_links` SET `html` = '$html' WHERE `id` = '{$record->id}'");
    print $record->id . "\n";*/
endwhile;